package com.googlecode.jlsf;

import java.util.ArrayList;

/**
 * shoutcaster (shoutcast server) implementation
 */
public class Shoutcaster 
{
	private boolean status; // true - active; false - unavailable
	private String notice1 = "";
	private String notice2 = "";
	private String name = "";
	private String genre = "";
	private String url = "";
	private String contentType = "";
	private boolean pub = false;
	private int metaint = 0;
	private int br = 0;
	
	public Shoutcaster(ArrayList<String> answerFromSource) throws IllegalArgumentException
	{
		if(answerFromSource.get(0).equalsIgnoreCase("ICY 200 OK"))
			status = true;
		else if(answerFromSource.get(0).equalsIgnoreCase("ICY 401 Service Unavailable"))
			status = false;
		else
			throw new IllegalArgumentException("Unable to create shoutcaster");
		
		for(String s : answerFromSource)
		{
			if(s.contains("icy-notice1:"))
			{
				notice1 = clearTag(s, "icy-notice1:");
				continue;
			}
			else if(s.contains("icy-notice2:"))
			{
				notice2 = clearTag(s, "icy-notice2:");
				continue;
			}
			else if(s.contains("icy-name:"))
			{
				name = clearTag(s, "icy-name:");
				continue;
			}
			else if(s.contains("icy-genre:"))
			{
				genre = clearTag(s, "icy-genre:");
				continue;
			}
			else if(s.contains("icy-url:"))
			{
				url = clearTag(s, "icy-url:");
				continue;
			}
			else if(s.contains("content-type:"))
			{
				contentType = clearTag(s, "content-type:");
				continue;
			}
			else if(s.contains("icy-pub:") && s.contains("0"))
			{
				pub = false;
				continue;
			}
			else if(s.contains("icy-pub:") && s.contains("1"))
			{
				pub = true;
				continue;
			}
			else if(s.contains("icy-metaint:"))
			{
				metaint = Integer.parseInt(clearTag(s, "icy-metaint:"));
				continue;
			}
			else if(s.contains("icy-br:"))
			{
				br = Integer.parseInt(clearTag(s, "icy-br:"));
				continue;
			}
		}
		
	}

	private String clearTag(String s, String tag) 
	{
		return s.split(tag)[1];
	}
	
	public String toString()
	{
		if(status)
			return name;
		else 
			return "Shoutcaster is unavailable";
	}
	
	public boolean isActive()
	{
		return status;
	}
	
	public String getNotice1()
	{
		return notice1;
	}
	
	public String getNotice2()
	{
		return notice2;
	}
	
	public String getGenre()
	{
		return genre;
	}
	
	public String getURL()
	{
		return url;
	}
	
	public String getContentType()
	{
		return contentType;
	}
	
	public boolean isPublic()
	{
		return pub;
	}
	
	public int getMetaInt()
	{
		return metaint;
	}
	
	public int getBitRate()
	{
		return br;
	}
	
}
